package com.game.login.service;

import com.baomidou.mybatisplus.toolkit.CollectionUtils;
import com.game.framework.dao.entity.*;
import com.game.framework.dao.mapper.*;
import com.game.framework.message.protobuf.BaseProto;
import com.game.framework.message.protobuf.ActivityProto;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Slf4j
@Service
public class ActivityServiceImpl implements ActivityService {

    @Autowired
    protected ActivityFishBaitMapper activityFishBaitMapper;
    @Autowired
    protected ActivityFishPoleMapper activityFishPoleMapper;
    @Autowired
    protected ActivityFishTypeMapper activityFishTypeMapper;
    @Autowired
    protected ActivityObjectMapper activityObjectMapper;
    @Autowired
    protected ActivityPlayerMapper activityPlayerMapper;
    @Autowired
    protected ActivityMapper activityMapper;
    @Autowired
    protected FishPoleMapper fishPoleMapper;
    @Autowired
    protected RoleMapper roleMapper;

    public ActivityProto.ActivityInfo generateActivityInfo(ActivityEntity activityEntity) {
        ActivityProto.ActivityInfo activityInfo = ActivityProto.ActivityInfo.newBuilder()
                .setId(activityEntity.getId())
                .setName(activityEntity.getName())
                .setLimitPlayer(activityEntity.getLimitPlayer())
                .setLimitTicketType(activityEntity.getLimitTicketType())
                .setLimitTicketNum(activityEntity.getLimitTicketNum())
                .setLimitPoleType(activityEntity.getLimitPoleType())
                .setLimitPoleNum(activityEntity.getLimitPoleNum())
                .setLimitDragonType(activityEntity.getLimitDragonType())
                .setLimitDragonNum(activityEntity.getLimitDragonNum())
                .setLimitObjects(activityEntity.getLimitObjects())
                .setCurPlayerNum(activityEntity.getCurPlayerNum())
                .setMaxPlayerNum(activityEntity.getMaxPlayerNum())
                .setStatus(activityEntity.getStatus())
                .build();
        if(ObjectUtils.isNotEmpty(activityEntity.getStartTime())) {
            activityInfo.toBuilder().setStartTime(activityEntity.getStartTime().getTime()).build();
        }
        if(ObjectUtils.isNotEmpty(activityEntity.getEndTime())) {
            activityInfo.toBuilder().setEndTime(activityEntity.getEndTime().getTime()).build();
        }
        return activityInfo;
    }

    public ActivityProto.FishInfo generateActivityFishTypeInfo(ActivityFishTypeEntity activityFishTypeEntity) {
        return ActivityProto.FishInfo.newBuilder()
                .setFishType(activityFishTypeEntity.getFishType())
                .setFishName(activityFishTypeEntity.getFishName())
                .setFishCount(activityFishTypeEntity.getFishCount())
                .setRemainCount(activityFishTypeEntity.getRemainCount())
                .build();
    }

    @Override
    public BaseProto.ErrorCode getActivityList(ActivityProto.GetActivityListRequest request, ActivityProto.GetActivityListResponse.Builder response) {
        //RoleEntity roleEntity = roleMapper.selectByRoleId(request.getRoleId());
        List<ActivityEntity> activityEntityList = activityMapper.selectAll();
        if(CollectionUtils.isNotEmpty(activityEntityList)){
            for(ActivityEntity activityEntity : activityEntityList){
                ActivityProto.ActivityInfo activityInfo;
                List<ActivityProto.FishInfo> fishInfoList = new ArrayList<>();
                List<ActivityFishTypeEntity> activityFishTypeEntityList = activityFishTypeMapper.selectByActivityId(activityEntity.getId());
                if(CollectionUtils.isNotEmpty(activityFishTypeEntityList)){
                    for (ActivityFishTypeEntity activityFishTypeEntity : activityFishTypeEntityList){
                        ActivityProto.FishInfo fishInfo = generateActivityFishTypeInfo(activityFishTypeEntity);
                        fishInfoList.add(fishInfo);
                    }
                }
                //TODO 暂时把限制条件去掉
//                if(activityEntity.getStatus() == 1) {
//                    if ((activityEntity.getLimitTicketNum() > roleEntity.getTicket())
//                            ||(activityEntity.getLimitDragon() > roleEntity.getRoleId())
//                            ||(activityEntity.getLimitPole() > roleEntity.getPole())) {
//                        activityEntity.setStatus(3);
//                    }
//                }
                activityInfo = generateActivityInfo(activityEntity);
                if(CollectionUtils.isNotEmpty(fishInfoList)) {
                    activityInfo.toBuilder().addAllFishInfos(fishInfoList);
                }
                response.addActivityInfos(activityInfo);
            }
        }
        return BaseProto.ErrorCode.SUCCESS;
    }

    @Override
    public void test(){
//        if(userInfoMapper == null){
//            log.error("userInfoMapper is not start!!!!");
//            userInfoMapper = applicationContext.getBean(UserInfoMapper.class);
//        }
//        UserInfoEntity userInfoEntity = userInfoMapper.selectByPhone("17521104340");
//        if (userInfoEntity != null){
//            log.info(userInfoEntity.toString());
//        }
//        if(roleMapper == null){
//            roleMapper = applicationContext.getBean(RoleMapper.class);
//        }
//        long userid = 296;
//        List<RoleEntity> roleEntityList = roleMapper.selectByUserId(userid);
//        if (roleEntityList != null){
//            log.info(roleEntityList.toString());
//        }
//        ActivityProto.GetActivityListRequest request = ActivityProto.GetActivityListRequest.newBuilder().build();
//        ActivityProto.GetActivityListResponse.Builder response = ActivityProto.GetActivityListResponse.newBuilder();
//        BaseProto.ErrorCode errorCode = getActivityList(request, response);
//        ByteString resBody = response.build().toByteString();
//        log.info(resBody.toString());
    }
}
